<template>
  <!--  留言框-->
  <div class="tip">
    <h1 class="title">留言</h1>
    <div class="send">
      <el-input
        v-model="Message"
        clearable
        placeholder="来都来了，不如留下点什么..."
        style="width: 240px"
      />
      <div class="iconfont icon-fasong2 send-icon" @click="sendMessage"></div>
    </div>
  </div>
  <!--  弹幕-->
  <vue-danmaku
    v-if="danmus.length"
    ref="vueDanmakuRef"
    v-model:danmus="danmus"
    :isSuspend="true"
    :loop="loop"
    :speeds="60"
    class="danmus"
    useSlot
  >
    <template v-slot:dm="{ danmu }">
      <div class="danmu-item">
        <el-avatar :src="danmu.avatar">{{ danmu.username }}</el-avatar>
        :
        <span v-if="danmu.isNew" class="new-item" v-html="danmu.message"></span>
        <span v-html="danmu.content"></span>
      </div>
    </template>
  </vue-danmaku>
  <!--  流星-->
  <div class="shooting_stars !z-[1]">
    <div class="night">
      <span v-for="i in 20" :key="i" class="shooting_star"></span>
    </div>
  </div>
</template>
<script setup>
import { getCurrentInstance, ref } from "vue";
import { message } from "ant-design-vue";
import VueDanmaku from "vue3-danmaku";
import { useStore } from "@/store/user.js";
import ua2obj from "ua2obj";
import { addMessage, getAllMessage } from "@/api/message.js";

const user = useStore();
let browserName = "";
let osName = "";

let Message = ref("");
const loop = ref(false);
const vueDanmakuRef = ref(null);
const danmus = ref([]);
// 发送留言
const sendMessage = async () => {
  if (Message.value !== "" && user.isLogin) {
    let res = await addMessage({
      content: Message.value,
      browserName,
      osName,
      user: 1,
    });
    if (res && res.status === 201) {
      const { avatar, content, username } = res.data;
      danmus.value.push({
        avatar: avatar,
        content,
        username,
        isNew: true,
      });
      Message.value = "";
      message.success("新增成功");
    } else {
      message.error(res.data);
    }
  } else message.error("留言必须先 登录 并且不能为空哦...");
};

// 获取所有的留言
const getMessages = async () => {
  let res = await getAllMessage();
  if (res && res.status === 200) {
    danmus.value = res.data.map((item) => {
      return {
        avatar: item.avatar,
        content: item.content,
        username: item.username,
        isNew: false,
      };
    });
  }
};

const init = async () => {
  await getMessages();
  browserName = ua2obj()["browserName"];
  osName = ua2obj()["osName"];
};
init();
</script>

<style lang="scss" scoped>
.tip {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  text-align: center;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 15px;
  padding: 35px;
  border-radius: 15px;
  backdrop-filter: blur(5px);

  .title {
    font-size: 35px;
    font-weight: bold;
    color: #ffff;
    margin-right: 20px;
  }

  .send {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;

    .send-icon {
      font-size: 30px;
      cursor: pointer;
    }
  }
}

.danmus {
  position: absolute;
  top: 64px;
  left: 0;
  width: 100%;
  height: calc(100vh - 64px);
  z-index: 99;

  .danmu-item {
    display: flex;
    align-items: center;
    justify-content: center;

    .new-item {
      position: relative;
      display: inline-block;
      border-radius: 8px;
      padding: 0 16px;

      &::after {
        content: "new";
        position: absolute;
        top: -10px;
        right: 0;
        color: #fff;
        background: skyblue;
        padding: 3px;
        border-radius: 8px;
        margin-right: 8px;
        font-size: 12px;
      }
    }
  }
}

.shooting_stars {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  background: radial-gradient(ellipse at bottom, #1b2735 0%, #090a0f 100%);
  overflow: hidden;
  display: flex;
  font-family: "Anton", sans-serif;
  justify-content: center;
  align-items: center;
}

.night {
  position: relative;
  width: 100%;
  height: 100%;
  transform: rotateZ(45deg);
}

.shooting_star {
  position: absolute;
  left: 10%;
  top: 20%;
  height: 2px;
  background: linear-gradient(
    -45deg,
    rgba(95, 145, 255, 1),
    rgba(0, 0, 255, 0)
  );
  border-radius: 999px;
  filter: drop-shadow(0 0 6px rgba(105, 155, 255, 1));
  animation: tail 5s ease-in-out infinite, shooting 5s ease-in-out infinite;

  &::before {
    content: "";
    position: absolute;
    top: calc(50% - 1px);
    right: 0;
    height: 2px;
    background: linear-gradient(
      -45deg,
      rgba(0, 0, 255, 0),
      rgba(95, 145, 255, 1),
      rgba(0, 0, 255, 0)
    );
    transform: translateX(50%) rotateZ(45deg);
    border-radius: 100%;
    animation: shining 3000 ease-in-out infinite;
  }

  &::after {
    content: "";
    position: absolute;
    top: calc(50% - 1px);
    right: 0;
    height: 2px;
    background: linear-gradient(
      -45deg,
      rgba(0, 0, 255, 0),
      rgba(95, 145, 255, 1),
      rgba(0, 0, 255, 0)
    );
    border-radius: 100%;
    animation: shining 3000 ease-in-out infinite;
    transform: translateX(50%) rotateZ(-45deg);
  }

  @for $i from 1 through 20 {
    &:nth-child(#{$i}) {
      $delay: random(9999) + 0ms;
      top: calc(50% - #{random(400) - 200px});
      left: calc(30% - #{random(300) + 0px});
      animation-delay: $delay;
      // opacity: random(50) / 100 + 0.5;

      &::before,
      &::after {
        animation-delay: $delay;
      }
    }
  }
}

@keyframes tail {
  0% {
    width: 0;
  }

  30% {
    width: 100px;
  }

  100% {
    width: 0;
  }
}

@keyframes shining {
  0% {
    width: 0;
  }

  50% {
    width: 30px;
  }

  100% {
    width: 0;
  }
}

@keyframes shooting {
  0% {
    transform: translateX(0);
  }

  100% {
    transform: translateX(800px);
  }
}

@keyframes sky {
  0% {
    transform: rotate(45deg);
  }

  100% {
    transform: rotate(45 + 360deg);
  }
}
</style>
